from collections.abc import Generator

from assertpy import assert_that
from playwright.sync_api import BrowserContext, Page
import pytest

import actions.colocation.colocation_monitor as cm

pytest.mark.usefixtures("login_and_store_state")


@pytest.fixture(scope='function')
def monitor_page(context: BrowserContext) -> Generator[Page, None, None]:
    """混部监控页面"""
    page = context.new_page()
    cm.visit_colocation_monitor_page(page)
    yield page
    page.close()


class TestMonitorPage:
    """
    @description 测试混部监控页面图表是否正常加载
    echarts图表在前端被渲染为 <canvas>，不暴露DOM元素，因此无法获取数据以测试正确性
    """
    @staticmethod
    def test_cluster_data_display(monitor_page: Page):
        """Test 111, 113, 115, 117, 119, 121, 123, 125"""
        cm.do_switch_to_cluster_monitor(monitor_page)
        paragraphs = cm.get_monitor_paragraphs(monitor_page)
        if len(paragraphs) <= 8:
            # 正常情况下时8个图表标题
            assert_that(paragraphs).is_length(8)    
        else:
            no_content_chart = []
            for i, p in enumerate(paragraphs):
                if p.text_content() == '暂无内容':
                    no_content_chart.append(paragraphs[i - 1].text_content())
            assert_that(False, f'以下图表没有数据: {", ".join(no_content_chart)}')

    @staticmethod
    def test_node_data_display(monitor_page: Page):
        """Test 128, 130, 132, 134, 136, 138"""
        cm.do_switch_to_node_monitor(monitor_page)
        paragraphs = cm.get_monitor_paragraphs(monitor_page)
        if len(paragraphs) <= 6:
            # 正常情况下时6个图表标题
            assert_that(paragraphs).is_length(6)    
        else:
            no_content_chart = []
            for i, p in enumerate(paragraphs):
                if p.text_content() == '暂无内容':
                    no_content_chart.append(paragraphs[i - 1].text_content())
            assert_that(False, f'以下图表没有数据: {", ".join(no_content_chart)}')
    